package com.freight.tws.controller;

import java.util.List;
import java.io.IOException;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;

import lombok.RequiredArgsConstructor;
import com.freight.common.security.annotation.PreAuthorize;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.freight.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.freight.common.log.annotation.Log;
import com.freight.common.log.enums.BusinessType;
import com.freight.common.core.web.controller.BaseController;
import com.freight.common.core.web.domain.AjaxResult;
import com.freight.common.core.utils.poi.ExcelUtil;
import com.freight.common.core.web.page.Query;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.freight.common.core.web.page.Condition;
import com.freight.tws.domain.TwsAddress;
import com.freight.tws.service.ITwsAddressService;
import com.freight.common.core.web.page.TableDataInfo;

/**
 * 发票收件管理Controller
 * 
 * @author wjx
 * @date 2023-02-22
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/address" )
public class TwsAddressController extends BaseController
{
    private final ITwsAddressService iTwsAddressService;

    /**
     * 查询发票收件管理列表
     */
    @PreAuthorize(hasPermi="tws:address:list")
    @GetMapping("/list")
    public TableDataInfo list(Query query, TwsAddress twsAddress)
    {
        LambdaQueryWrapper<TwsAddress> lqw = new LambdaQueryWrapper<TwsAddress>();
        if (StringUtils.isNotBlank(twsAddress.getAddressee())) {
            lqw.eq(TwsAddress::getAddressee, twsAddress.getAddressee());
        }
        if (StringUtils.isNotBlank(twsAddress.getAddress())) {
            lqw.eq(TwsAddress::getAddress, twsAddress.getAddress());
        }
        if (StringUtils.isNotBlank(twsAddress.getPhone())) {
            lqw.eq(TwsAddress::getPhone, twsAddress.getPhone());
        }
        if (twsAddress.getIsCommon() != null) {
            lqw.eq(TwsAddress::getIsCommon, twsAddress.getIsCommon());
        }
        if (StringUtils.isNotBlank(twsAddress.getTenantId())) {
            lqw.eq(TwsAddress::getTenantId, twsAddress.getTenantId());
        }
        lqw.orderByDesc(TwsAddress::getCreateTime);
        IPage<TwsAddress> list = iTwsAddressService.page(Condition.getPage(query), lqw);
        return getDataTable(list);
    }

    /**
     * 导出发票收件管理列表
     */
    @PreAuthorize(hasPermi = "tws:address:export")
    @Log(title = "发票收件管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TwsAddress twsAddress) throws IOException
    {
        LambdaQueryWrapper<TwsAddress> lqw = new LambdaQueryWrapper<TwsAddress>(twsAddress);
        List<TwsAddress> list = iTwsAddressService.list(lqw);
        ExcelUtil<TwsAddress> util = new ExcelUtil<TwsAddress>(TwsAddress. class);
        util.exportExcel(response, list, "address");
    }

    /**
     * 获取发票收件管理详细信息
     */
    @PreAuthorize(hasPermi="tws:address:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(iTwsAddressService.getById(id));
    }

    /**
     * 新增发票收件管理
     */
    @PreAuthorize(hasPermi="tws:address:add")
    @Log(title = "发票收件管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TwsAddress twsAddress) {
        return toAjax(iTwsAddressService.save(twsAddress) ? 1 : 0);
    }

    /**
     * 修改发票收件管理
     */
    @PreAuthorize(hasPermi="tws:address:edit")
    @Log(title = "发票收件管理" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TwsAddress twsAddress) {
        return toAjax(iTwsAddressService.updateById(twsAddress) ? 1 : 0);
    }

    /**
     * 删除发票收件管理
     */
    @PreAuthorize(hasPermi="tws:address:remove")
    @Log(title = "发票收件管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iTwsAddressService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

}
